<?php
    class BabyController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        
        public function init()
        {
            $this->loadTemplates();
             $this->_arrParam = $this->_request->getParams();
             $this->_model = new Default_Model_Baby();
        }
        
        public function indexAction()
        {
            $this->view->headTitle('t\'house - Đồ trẻ em');
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->getListItem());
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(30);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
        }
        
        public function categoryAction()
        {
            $id = (int) $this->_arrParam['id'];
            $category = new Default_Model_Category();
            $this->view->categoryName = $category->getName($id, 'baby_category');
            $this->view->headTitle('t\'house - '.$this->view->categoryName);
            $result = $category->getChild($id, 'baby_category');
            if(count($result) > 0) {
                $this->view->category = $result;
                $this->render('list');
            }
            else {
                $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->getItemByCategory($id));
                $paginator = new Zend_Paginator($adapter);
                $paginator->setItemCountPerPage(30);
                $paginator->setPageRange(5);
                $currentPage = $this->_request->getParam('page', 1);
                $paginator->setCurrentPageNumber($currentPage);
                $this->view->data = $paginator;
                $this->render('category');
            }
        }
        
        public function detailAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->view->data = $this->_model->getItemById($id);
            $this->view->headTitle('t\'house - '.$this->view->data['name']);
            $this->view->baby = $this->_model->getItemContinue($id, $this->view->data['category_id']);
        }
    }
